// 工具类
const tools = {
  formatDate: (date, format = 'yyyy-MM-dd hh:mm:ss') => {
    if (!date) {
      return ''
    }
    let info = date;
    // 如果是时间戳，就转换为日期对象
    if (typeof date === 'number') {
      info = new Date();
      info.setTime(date);
    }
    // 格式化日期为字符串
    let year = info.getFullYear();
    let month = info.getMonth() + 1;
    let day = info.getDate();
    let hour = info.getHours();
    let minute = info.getMinutes();
    let second = info.getSeconds();
    let result = format.replace('yyyy', year);
    result = result.replace('MM', month < 10 ? '0' + month : month);
    result = result.replace('dd', day < 10 ? '0' + day : day);
    result = result.replace('hh', hour < 10 ? '0' + hour : hour);
    result = result.replace('mm', minute < 10 ? '0' + minute : minute);
    result = result.replace('ss', second < 10 ? '0' + second : second);
    return result;





  },
  // 合并多个json为一个
  mergeJson: (...jsons) => {
    let result = {};
    // 循环获取多个json值
    for (let json of jsons) {
      // 循环获取json的key和数据
      for (let key in json) {
        result[key] = json[key];
      }
    }
    return result;
  },
  // 浏览文件
  openFile: (cb, accept = '') => {
    // 创建文件元素
    const fileinput = document.createElement('input')
    fileinput.setAttribute('type', 'file')
    fileinput.setAttribute('accept', accept)
    fileinput.addEventListener('change', () => {
      console.log('选中的图片:', fileinput.files)
      if (fileinput.files && fileinput.files.length) {
        cb(fileinput.files[0])
      }
    })
    fileinput.click()
  },
  // 浏览图片
  openImage: cb => {
    tools.openFile(cb, 'image/*')
  },

}

export { tools as default, tools as tools };